<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Article_model extends CI_Model {
	
	function __construct(){
        parent::__construct();
    }
    
    
	function arrays($key){
    	$data = array(
    		'article_show' 			=> array("0"=>"显示", "1"=>"不显示"),
    	);
    	
    	if($key && isset($data[$key])){
    		return $data[$key];
    	}else{
    		return array();
    	}
    }
    
	/**
	 * 发布文章
	 * @param $data
	 */
	function add($data = array()){
		$this->mycache->delete('get_article_list_index');
		$this->mycache->delete('get_article_list_index2');
		$this->db->insert('rsh_article', $data);
		return $this->db->insert_id();
	}
	

	function update($id, $data = array()){
		$this->mycache->delete('get_article_list_index');
		$this->mycache->delete('get_article_list_index2');
		$this->mycache->delete('article_obj_'.$id);
		$this->db->where('id', $id);
		$this->db->update('rsh_article', $data);
		return $this->db->affected_rows();
	}
	
	function get_article_obj($id){
		$mkey = "article_obj_".$id;
		//$this->mycache->delete($mkey);
		$ret = $this->mycache->get_array($mkey);
		
		if($ret != false){
			return $ret;
		}
		$select = "id,title,nid,ptime,author,forder,fsource,thumbnail,summary,content,add_time,is_show,hits,is_recommend,seo_keyword";
		$this->db->select($select);
		$this->db->where(array('id'=>$id));
		$query = $this->db->get('rsh_article');
		$retsult = $query->row_array();
		$this->mycache->set_array($mkey,$retsult);
		return $retsult;
	}
    
	function get_article_list_index_limit($data){
		
		//封装查询内容
		$select = "id,title,ptime,author,forder,content,thumbnail,summary,add_time";
		
		//封装条件
		$_where = " where is_del=0 ";
		if(isset($data['where']['nid']) && $data['where']['nid'] != ''){
			$_where .= " and nid=".$data['where']['nid'];
		}

		//排序
		$order  = " order by forder,add_time desc ";

		$sql_result = "select $select from rsh_article";
		$sql_result .= $_where;
		$sql_result .= $order;
		$sql_result .= " limit 0, 15";
		$query = $this->db->query($sql_result);
		$article_list = $query->result_array();
		return $article_list;
	}
	
	function get_article_list_index($data){
		$mkey = "get_article_list_index";
		//$this->mycache->delete($mkey);
		$ret = $this->mycache->get_array($mkey);
		if($ret != false){
			return $ret;
		}
		//封装查询内容
		$select = "id,title,ptime,author,forder,content,thumbnail,summary,add_time";
		
		//封装条件
		$_where = " where is_del=0 ";
		if(isset($data['where']['nid']) && $data['where']['nid'] != ''){
			$_where .= " and nid=".$data['where']['nid'];
		}
		if(isset($data['where']['title']) && $data['where']['title'] != ''){
			$_where .= " and title like '%".$data['where']['title']."%'";
		}
		
		//排序
		$order  = " order by add_time desc ";
		//封装sql
		$page_size = $data['page_size'];
		$page_start = $data['page_start'];
		$sql_result = "select $select from rsh_article";
		$sql_result .= $_where;
		$sql_result .= $order;
		$sql_result .= " limit $page_start, $page_size";
		$query = $this->db->query($sql_result);
		$article_list = $query->result_array();
		$this->mycache->set_array($mkey,$article_list);
		return $article_list;
	}
	
	function get_article_list_index2($data){
		$mkey = "get_article_list_index2";
		//$this->mycache->delete($mkey);
		$ret = $this->mycache->get_array($mkey);
		if($ret != false){
			return $ret;
		}
		//封装查询内容
		$select = "id,title,ptime,author,forder,fsource,thumbnail,summary,content";
		
		//封装条件
		$_where = " where is_del=0 ";
		if(isset($data['where']['nid']) && $data['where']['nid'] != ''){
			$_where .= " and nid=".$data['where']['nid'];
		}
		if(isset($data['where']['title']) && $data['where']['title'] != ''){
			$_where .= " and title like '%".$data['where']['title']."%'";
		}
		
		//排序
		$order  = " order by id desc ";
		//封装sql
		$page_size = $data['page_size'];
		$page_start = $data['page_start'];
		$sql_result = "select $select from rsh_article";
		$sql_result .= $_where;
		$sql_result .= $order;
		$sql_result .= " limit $page_start, $page_size";
		$query = $this->db->query($sql_result);
		$article_list = $query->result_array();
		$this->mycache->set_array($mkey,$article_list);
		return $article_list;
	}
	
	function get_article_list($data){
		//封装查询内容
		$select = "id,title,nid,ptime,author,forder,fsource,summary,thumbnail,content";
		
		//封装条件
		$_where = " where is_del=0 ";
		if(isset($data['where']['nid']) && $data['where']['nid'] != ''){
			$_where .= " and nid=".$data['where']['nid'];
		}
		if(isset($data['where']['title']) && $data['where']['title'] != ''){
			$_where .= " and title like '%".$data['where']['title']."%'";
		}
		if(isset($data['where']['txt']) && $data['where']['txt'] != ''){
			$_where .= " and (title like '%".$data['where']['txt']."%' or content like '%".$data['where']['txt']."%')";
		}
		//排序
		$order  = " order by id desc ";
		//封装sql
		$page_size = $data['page_size'];
		$page_start = $data['page_start'];
		$sql_result = "select $select from rsh_article";
		$sql_result .= $_where;
		$sql_result .= $order;
		$sql_result .= " limit $page_start, $page_size";
		$query = $this->db->query($sql_result);
		$data['article_list'] = $query->result_array();
		
		
		//echo $sql_result;exit;
		//总记录条数
		$sql_count = "select count(*) as count from rsh_article";
		$sql_count .= $_where;
		$query = $this->db->query($sql_count);
		$data['article_count'] = $query->row()->count;
		return $data;
	}
	
	function get_new_article_list($data){
		
		//封装条件
		$_where = " where a.is_del=0 ";
		if(isset($data['where']['nid']) && $data['where']['nid'] != ''){
			$_where .= " and a.nid=".$data['where']['nid'];
		}
		if(isset($data['where']['title']) && $data['where']['title'] != ''){
			$_where .= " and a.title like '%".$data['where']['title']."%'";
		}
		
		
		//排序
		$order  = " order by a.id desc ";
		
		//封装sql
		$page_size = $data['page_size'];
		$page_start = $data['page_start'];
		$sql_result = "select a.*,s.title as name,s.parent_id from rsh_article as a left join rsh_site as s on a.nid=s.id";
		$sql_result .= $_where;
		$sql_result .= $order;
		$sql_result .= " limit $page_start, $page_size";
		$query = $this->db->query($sql_result);
		$data['article_list'] = $query->result_array();
		
		//总记录条数
		$sql_count = "select count(*) as count from rsh_article as a ";
		$sql_count .= $_where;
		$query = $this->db->query($sql_count);
		$data['article_count'] = $query->row()->count;
		return $data;
	}
}